package com.camera.yl.v

import android.view.View
import androidx.media3.common.MediaItem
import androidx.media3.common.PlaybackException
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import com.alibaba.android.arouter.facade.annotation.Route
import com.camera.base.v.BaseActivity
import com.camera.routemodule.RouterPath
import com.camera.yl.R
import com.camera.yl.databinding.ActivityMediaPlayBinding
import com.camera.yl.v.models.MediaPlayViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.path.mediaplay_activity)
class MediaPlayActivity: BaseActivity<ActivityMediaPlayBinding, MediaPlayViewModel>() {
    private val TAG = "MeidaSettingActivity";
    private lateinit var url: String
    override val viewModelClass: Class<MediaPlayViewModel>
        get() = MediaPlayViewModel::class.java
    override fun getLayoutId(): Int {
        return R.layout.activity_media_play
    }

    override fun getStatusBarColor(): Int {
        return R.color.title_background_color
    }

    override fun loadData() {
        url = intent.getStringExtra("path").toString()
        mBinding.playView.player = ExoPlayer.Builder(this).build()
        mBinding.playView.player?.run {
            // 设置播放监听
            addListener(object : Player.Listener {
                override fun onIsPlayingChanged(isPlaying: Boolean) {
                    super.onIsPlayingChanged(isPlaying)
                    // 播放状态变化回调
                }

                override fun onPlaybackStateChanged(playbackState: Int) {
                    super.onPlaybackStateChanged(playbackState)
                    when (playbackState) {
                        Player.STATE_IDLE -> {
                            //播放器停止时的状态
                        }

                        Player.STATE_BUFFERING -> {
                            // 正在缓冲数据
                        }

                        Player.STATE_READY -> {
                            // 可以开始播放
                        }

                        Player.STATE_ENDED -> {
                            // 播放结束
                        }
                    }

                }

                override fun onPlayerError(error: PlaybackException) {
                    super.onPlayerError(error)
                    // 获取播放错误信息
                }
            })
            // 设置重复模式
            // Player.REPEAT_MODE_ALL 无限重复
            // Player.REPEAT_MODE_ONE 重复一次
            // Player.REPEAT_MODE_OFF 不重复
            repeatMode = Player.REPEAT_MODE_ALL
            // 设置当缓冲完毕后直接播放视频
            playWhenReady = true
        }
        mBinding.playView.player?.run {
            stop()
            setMediaItem(MediaItem.fromUri(url))
            prepare()
        }
    }

    override fun initView() {

    }
//    @SingleClick
    fun myClick(view: View) {
        when (view.id) {
            R.id.btn_video_sub_strame_rate_set -> {
            }
        }
    }

    override fun onResume() {
        super.onResume()
        mBinding.playView.onResume()
    }
    override fun onPause() {
        super.onPause()
        // 暂停播放
        mBinding.playView.onPause()
    }
    override fun onDestroy() {
        super.onDestroy()
        // 释放播放器资源
        mBinding.playView.player?.release()
        mBinding.playView.player = null
    }
}